<?php

namespace plugin\admin\app\controller;

use app\service\MemberWalletService;
use plugin\admin\app\model\RlMember;
use plugin\admin\app\model\RlMemberWallet;
use support\Db;
use support\facade\Logger;
use support\Request;
use support\Response;
use plugin\admin\app\model\RlMemberWithdrawal;
use support\exception\BusinessException;
use const support\CORRECT;
use const support\direction_in;
use const support\direction_out;
use const support\wallet_types_1;

/**
 * 提现列表 
 */
class RlMemberWithdrawalController extends Crud
{
    
    /**
     * @var RlMemberWithdrawal
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlMemberWithdrawal;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-member-withdrawal/index');
    }

    /**
     * 获取
     * @return Response
     */
    public function select(Request $request): Response
    {
        $page = (int)$request->get('page');
        $limit = (int)$request->get('limit', 10);
        $where['status']=1;
        $count=RlMemberWithdrawal::count();
        $offset=($page-1)*$limit;
        $lists= RlMemberWithdrawal::orderBy('id','desc')
            ->offset($offset)->limit($limit)->get()->toArray();
        //获取用户ID
        $memberIds=array_column($lists,'member_id');
        $members=RlMember::whereIn('id',$memberIds)->get()->toArray();
        $members=array_column($members,null,'id');
        //进行昵称头像追加
        foreach ($lists as &$list){
            $list['nickname']=$members[$list['member_id']]['nickname']??'';
            $list['avatar']=$members[$list['member_id']]['avatar']??'';
        }
        return json(['code' => 0, 'data' => $lists, 'count'=>$count,'msg' => '成功']);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('rl-member-withdrawal/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('rl-member-withdrawal/update');
    }

    //更新转账状态
    public function transferUpdate(Request $request): Response
    {
        $id=$request->post('id');
        $status=(int)$request->post('status');
        Db::beginTransaction();
        try {
            $withdrawal=RlMemberWithdrawal::where('id',$id)->first();
            if (empty($withdrawal->id)){
                return $this->json(-1,'ID无效');
            }
            $withdrawal=$withdrawal->toArray();
            $update=[
                'status'=>$status,
                'update_time'=>time()
            ];
            if ($status==1){
                $update['audit_time']=time();
            }elseif ($status==-1){
                $update['audit_time']=time();
            }elseif ($status==2){
                $update['transfer_time']=time();
            }
            Logger::app('更新转账状态：更新信息信息','info',$update);
            $ret=RlMemberWithdrawal::where('id',$id)->update($update);
            if (!$ret){
                Db::rollBack();
                return $this->json(-1,'更新提现状态失败');
            }
            if (in_array($status,[1,-1])){
                Logger::app('更新转账状态：进入账户金额变动','info',[$status]);
                $direction=($status==1)?direction_out:direction_in;
                MemberWalletService::toWithdrawalMemberWalletData(
                    wallet_types_1,
                    $withdrawal['member_id'],
                    $direction,
                    $withdrawal['withdrawal_amount'],
                    $withdrawal['withdrawal_sn'],
                    $withdrawal['id'],
                );
            }
            Db::commit();
            return $this->json(0,'更新提现状态成功');
        }catch (\Throwable $e){
            Db::rollBack();
            Logger::app($e);
            return $this->json(-1,'更新提现状态异常');
        }
    }

}
